package cn.edu.qzuie.artist.module.td.controller.admin.region;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;



import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.region.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.region.RegionDO;
import cn.edu.qzuie.artist.module.td.service.region.RegionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - TD地域")
@RestController
@RequestMapping("/td/region")
@Validated
public class RegionController {

    @Resource
    private RegionService regionService;

    @PostMapping("/create")
    @Operation(summary = "创建地域")
    @PreAuthorize("@ss.hasPermission('td:region:create')")
    public CommonResult<Long> createRegion(@Valid @RequestBody RegionSaveReqVO createReqVO) {
        return success(regionService.createRegion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新地域")
    @PreAuthorize("@ss.hasPermission('td:region:update')")
    public CommonResult<Boolean> updateRegion(@Valid @RequestBody RegionSaveReqVO updateReqVO) {
        regionService.updateRegion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除地域")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:region:delete')")
    public CommonResult<Boolean> deleteRegion(@RequestParam("id") Long id) {
        regionService.deleteRegion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得地域")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:region:query')")
    public CommonResult<RegionRespVO> getRegion(@RequestParam("id") Long id) {
        RegionDO region = regionService.getRegion(id);
        return success(BeanUtils.toBean(region, RegionRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得地域列表")
    @PreAuthorize("@ss.hasPermission('td:region:query')")
    public CommonResult<List<RegionRespVO>> getRegionList(@Valid RegionListReqVO listReqVO) {
        List<RegionDO> list = regionService.getRegionList(listReqVO);
        return success(BeanUtils.toBean(list, RegionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出地域 Excel")
    @PreAuthorize("@ss.hasPermission('td:region:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRegionExcel(@Valid RegionListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<RegionDO> list = regionService.getRegionList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "地域.xls", "数据", RegionRespVO.class,
                        BeanUtils.toBean(list, RegionRespVO.class));
    }
    @GetMapping("/getReginByParentId")
    @Operation(summary = "根据父编号获取地域信息")
    @Parameter(name = "parentId", description = "父编号", required = true, example = "0")
    @PreAuthorize("@ss.hasPermission('td:region:query')")
    public CommonResult<List<RegionListSelectReqVo>> getReginByParentId(@RequestParam(value = "parentId",required = false,defaultValue ="0") Long parentId){
        List<RegionDO> list=regionService.getReginByParentId(parentId);
        return success(BeanUtils.toBean(list,RegionListSelectReqVo.class));
    }

}